<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="用户">
              <j-search-select-tag placeholder="请选择用户" v-model="queryParam.sysUserId" dict="sys_user,realname,id"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="订单编号">
              <a-input placeholder="请输入订单编号" v-model="queryParam.orderNum"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="订单状态">
                <j-dict-select-tag placeholder="请选择订单状态" v-model="queryParam.orderStatus" dictCode="order_status"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="预留姓名">
                <a-input placeholder="请输入预留姓名" v-model="queryParam.uname"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="预留电话">
                <a-input placeholder="请输入预留电话" v-model="queryParam.uphone"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="开始时间">
                <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间" class="query-group-cust" v-model="queryParam.startTime_begin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间" class="query-group-cust" v-model="queryParam.startTime_end"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="结束时间">
                <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间" class="query-group-cust" v-model="queryParam.endTime_begin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间" class="query-group-cust" v-model="queryParam.endTime_end"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="座位号">
                <j-search-select-tag placeholder="请选择座位号" v-model="queryParam.seatId" dict="sr_seat,name,id"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="区域">
                <j-search-select-tag placeholder="请选择区域" v-model="queryParam.quyuId" dict="sr_quyu,name,id"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-has="'seatorder:add'" @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button v-has="'seatorder:add'" type="primary" icon="download" @click="handleExportXls('订单')">导出</a-button>
   <!--   <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <!-- <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="remark" slot-scope="text">
          <a-tooltip v-if="text">
            <template slot="title">
              {{text||""}}
            </template>
            {{ (text||"").substring(0,9)+'...' }}
          </a-tooltip>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="footer" slot-scope="pageData">
          合计金额:{{summyData(pageData)}}
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-if="record.orderStatus == '2' || record.orderStatus == '3'">
                <a @click="handleBackMoney(record)">退款</a>
              </a-menu-item>
              <!-- <a-menu-item v-if="record.orderStatus != 0">
                <a-popconfirm title="确定退款吗?" @confirm="() => handleBackMoney(record)">
                  <a>退款</a>
                </a-popconfirm>
              </a-menu-item> -->
             <!-- <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item> -->
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <order-modal ref="modalForm" @ok="modalFormOk"></order-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import OrderModal from './modules/OrderModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import { postAction } from '@/api/manage'
  import { colAuthFilter } from "@/utils/authFilter"
  export default {
    name: 'OrderList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      OrderModal
    },
    data () {
      return {
        description: '订单管理页面',
        orderMoney: 0.00,
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'订单编号',
            align:"center",
            dataIndex: 'orderNum'
          },
          {
            title:'用户',
            align:"left",
            dataIndex: 'sysUserId_dictText'
          },          
          {
            title:'订单名称',
            align:"center",
            dataIndex: 'orderName'
          },
          {
            title:'订单状态',
            align:"center",
            dataIndex: 'orderStatus_dictText'
          },
          {
            title:'付款方式',
            align:"center",
            dataIndex: 'payType_dictText'
          },
          {
            title:'卡券名称',
            align:"center",
            dataIndex: 'myCouponId_dictText'
          },
          {
            title:'预留姓名',
            align:"center",
            dataIndex: 'uname'
          },
          {
            title:'预留电话',
            align:"center",
            dataIndex: 'uphone'
          },
          {
            title:'付款时间',
            align:"center",
            dataIndex: 'payTime'
          },
          {
            title:'订单金额',
            align:"center",
            dataIndex: 'orderMoney',
          },
          {
            title:'开始时间',
            align:"center",
            dataIndex: 'startTime'
          },
          {
            title:'结束时间',
            align:"center",
            dataIndex: 'endTime'
          },
          {
            title:'座位号',
            align:"center",
            dataIndex: 'seatId_dictText'
          },
          {
            title:'订单类型',
            align:"center",
            dataIndex: 'reserveType_dictText'
          },
          {
            title:'区域',
            align:"center",
            dataIndex: 'quyuId_dictText'
          },
          {
            title:'自习室',
            align:"center",
            dataIndex: 'zixishiId_dictText'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark',
            scopedSlots: { customRender: 'remark' }
          },
          {
            title:'创建日期',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title:'更新日期',
            align:"center",
            dataIndex: 'updateTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        /* 排序参数 */
        isorter:{
          column: 'field(order_status-5-4-3-0-1-2),startTime',
          order: 'desc',
        },
        url: {
          list: "/order/order/list",
          delete: "/order/order/delete",
          deleteBatch: "/order/order/deleteBatch",
          exportXlsUrl: "/order/order/exportXls",
          importExcelUrl: "order/order/importExcel",
          
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.disableMixinCreated=true;
      this.columns = colAuthFilter(this.columns,'seatorder:');
      this.loadData();
      this.initDictConfig();
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
      
    },
    methods: {
      summyData(pageData){
        let total = 0;
        pageData.forEach(item=>{
          total = total + Number(item.orderMoney)
        })
        return total.toFixed(2);
      },
      handleBackMoney(record){
        record.amt = record.orderMoney
        this.$confirm({
          title: '订单退款',
          content: h => (
            <div style="display:flex;align-items:center">
                <span style="flex:none;padding-right:16px">请输入退款金额</span>
                <input id="input-money" value={record.amt} onchange={(e)=>{
                  record.amt = document.getElementById("input-money").value;
                } }></input>
            </div>
          ),
          onOk:() => {
            console.log(record.amt);
            var that = this;
            postAction('order/order/backMoney', {
              orderId: record.id,
              money: record.amt
            }).then((res) => {
              console.log(res);
              if (res.success) {
                that.$message.success(res.message);
                that.loadData();
              } else {
                that.$message.warning(res.message);
              }
            });
          },
          onCancel:() => {
            
          },
        });

      },
      // handleBackMoney(record){
      //   var that = this;
      //   postAction('order/order/backMoney', record).then((res) => {
      //     console.log(res)
      //     if (res.success) {
      //       that.$message.success(res.message);
      //       that.loadData();
      //     } else {
      //       that.$message.warning(res.message);
      //     }
      //   });
      // },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'orderNum',text:'订单编号',dictCode:''})
        fieldList.push({type:'string',value:'sysUserId',text:'用户',dictCode:"sys_user,username,id"})
        fieldList.push({type:'string',value:'orderName',text:'订单名称',dictCode:''})
        fieldList.push({type:'string',value:'orderStatus',text:'订单状态',dictCode:'order_status'})
        fieldList.push({type:'string',value:'payType',text:'付款方式',dictCode:'pay_type'})
        fieldList.push({type:'string',value:'uname',text:'预留姓名',dictCode:''})
        fieldList.push({type:'string',value:'uphone',text:'预留电话',dictCode:''})
        fieldList.push({type:'datetime',value:'payTime',text:'付款时间'})
        fieldList.push({type:'string',value:'orderMoney',text:'订单金额',dictCode:''})
        fieldList.push({type:'datetime',value:'startTime',text:'开始时间'})
        fieldList.push({type:'datetime',value:'endTime',text:'结束时间'})
        fieldList.push({type:'string',value:'seatId',text:'座位号',dictCode:"sr_seat,name,id"})
        fieldList.push({type:'string',value:'quyuId',text:'区域',dictCode:"sr_quyu,name,id"})
        fieldList.push({type:'string',value:'zixishiId',text:'自习室',dictCode:"sr_zixishi,name,id"})
        fieldList.push({type:'string',value:'reserveType',text:'订单类型',dictCode:'reserve_type'})
        fieldList.push({type:'string',value:'orderSource',text:'订单来源',dictCode:'order_source'})
        fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
        fieldList.push({type:'datetime',value:'createTime',text:'创建时间',dictCode:''})
        fieldList.push({type:'datetime',value:'updateTime',text:'更新时间',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>